'libtgfx - Threaded Graphics library for FreeBASIC
'Copyright (C) 2013  Ebben Feagan <sir@mud.owlbox.net>
'
'This library is free software; you can redistribute it and/or
'modify it under the terms of the GNU Lesser General Public
'License as published by the Free Software Foundation; either
'version 2.1 of the License, or (at your option) any later version.
'
'This library is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
'Lesser General Public License for more details.
'
'You should have received a copy of the GNU Lesser General Public
'License along with this library; if not, write to the Free Software
'Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#include once "tgfx.bi"
#include once "tgfx-priv.bi"

dim shared sptr as any ptr

namespace tgfx

private sub do_circle( byval m as Message ptr )
    var i = cast(line_data ptr,m->x->d)
    if i->box then
        ..circle i->d,(i->f.x,i->f.y),i->s.x, i->c
        ..paint i->d,(i->f.x,i->f.y),i->c,i->c
    else
        ..circle i->d,(i->f.x,i->f.y),i->s.x, i->c
    end if
end sub

private sub do_line( byval m as Message ptr )
    var i = cast(line_data ptr,m->x->d)
    if i->box then
        if i->fill then
            ..line i->d,(i->f.x,i->f.y)-(i->s.x,i->s.y),i->c,BF
        else
            ..line i->d,(i->f.x,i->f.y)-(i->s.x,i->s.y),i->c,B
        end if
    else
        ..line i->d,(i->f.x,i->f.y)-(i->s.x,i->s.y),i->c
    end if
end sub

private sub do_init( byval m as Message ptr )
    var i = cast(init_data ptr,m->x->d)
    screenres i->d.x, i->d.y, i->m,,i->f
    sptr = screenptr
    if sptr <> 0 then
        m->m->send(new Message(_MTRUE))
    else
        m->m->send(new Message(_MFALSE))
    end if
end sub

sub GfxHandler( byval _u as any ptr )

    while 1

        var m = Comm->recv()
        if m <> 0 then
            select case m->c
            case _MEXIT
                delete m
                exit while

            case _MROUIT
                var i = cast(sub_data ptr, m->x->d)
                i->d(i->x)

            case _MINIT
                do_init(m)

            case _MTITLE
                var i = cast(str_data ptr,m->x->d)
                windowtitle *(i->d)

            case _MSPTR
                m->m->send(new Message(_MTRUE,sptr))

            case _MDRAW
                var i = cast(str_data ptr,m->x->d)
                dim as string temp
                temp = *(i->d)
                var r = i->p
                draw r, temp

            case _MPRINT
                var i = cast(str_data ptr,m->x->d)
                dim as string temp
                temp = *(i->d)
                ..draw string i->p, (i->l.x,i->l.y),temp,rgb(255,255,255)

            case _MLINE
                do_line(m)

            case _MCIRCLE
                do_circle(m)

            case _MCLS
                screensync
                cls

            case _MLOCK
                screenlock

            case _MULOCK
                screenunlock

            case _MGET
                var i = cast(line_data ptr,m->x->d)
                var t_c = cast(fb.image ptr,i->c)
                ..get t_c, (i->f.x,i->f.y)-(i->s.x,i->s.y),i->d
                m->m->send(new Message(_MTRUE))

            case _MPUT
                var i = cast(line_data ptr,m->x->d)
                var t_c = cast(fb.image ptr,i->c)
                ..put t_c, (i->f.x,i->f.y), i->d, TRANS
                m->m->send(new Message(_MTRUE))

            case _MCREATE
                var i = cast(line_data ptr, m->x->d)
                var ret = imagecreate(i->f.x,i->f.y,i->c)
                m->m->send(new Message(_MTRUE,new MData(ret,@null_free)))

            case _MKEYG
                var i = cast(int_data ptr, m->x->d)
                var ret = multikey(i->d)
                if ret = -1 then
                    m->m->send(new Message(_MTRUE))
                else
                    m->m->send(new Message(_MFALSE))
                end if

            case _MMSGET
                dim as integer x, y, wheel, btns, clip
                getmouse(x,y,wheel,btns,clip)
                var r = new line_data(0,Coord(x,y),Coord(wheel,btns),clip,tgfx.false,tgfx.false)
                m->m->send(new Message(_MMSGET,new MData(r,@null_free)))

            case _MMSSET
                var i = cast(line_data ptr,m->x->d)
                setmouse(i->f.x,i->f.y,i->s.x,i->s.y)

            case else
                var co = freefile
                open cons for output access write as #co
                print #co, "Unknown message: " & m->c
                close
            end select
            delete m
        end if

    wend

end sub

end namespace 'tgfx
